import Vue from 'vue'
import Router from 'vue-router'
import layout from '@/components/LayOut.vue'
import reg from '@/components/reg.vue'
import login from '@/components/login.vue'
import allAccount from '@/components/allAccount.vue'
import myAccount from '@/components/myAccount.vue'
import regTele from '@/components/regTele.vue'
import bacth from '@/components/bacth.vue'
import chat from '@/components/chat.vue'
import cash from '@/components/cash.vue'

Vue.use(Router)

const router = new Router({
  routes: [{
    path: '/',
    name: 'layout',
    component: layout,
    children: [{
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/reg',
      name: 'reg',
      component: reg
    },
    {
      path: '/myAccount',
      name: 'myAccount',
      component: myAccount
    },
    {
      path: '/allAccount',
      name: 'allAccount',
      component: allAccount
    },
    {
      path: '/regTele',
      name: 'regTele',
      component: regTele
    },
    {
      path: '/bacth',
      name: 'bacth',
      component: bacth
    },
    {
      path: '/chat',
      name: 'chat',
      component: chat
    },
    {
      path: '/cash',
      name: 'cash',
      component: cash
    }
    ]
  }]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === 'reg') {
    next()
  } else {
    let userJsonStr = sessionStorage.getItem('user')
    if (userJsonStr) {
      // let userEntity = JSON.parse(userJsonStr)
      // console.log(userEntity)
      next()
    } else {
      next('login')
    }
  }
})
export default router
